package com.unionServer.common;

/**
 * @description:
 * @author: wangxinal
 * @time: 2022/1/18 8:49
 */
public class ActualBinaryConvertor {
    public ActualBinaryConvertor() {
    }

    public byte[] getFromBcd(byte[] arg0) {
        StringBuilder sb = new StringBuilder();
        byte[] arr$ = arg0;
        int len$ = arg0.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Byte b = arr$[i$];
            String tmp = Integer.toBinaryString(b & 255);
            int length = tmp.length();
            if (length < 8) {
                for(int j = 0; j < 8 - length; ++j) {
                    sb.append("0");
                }
            }

            sb.append(tmp);
        }

        return sb.toString().getBytes();
    }

    public byte[] toBytes(Object value, MetaDataDataType valueType) {
        if (value != null) {
            String s = String.valueOf(value);
            byte[] bytes = new byte[s.length() / 8];
            int i = bytes.length - 1;

            for(int l = s.length(); l > 0; l -= 8) {
                if (l - 8 >= 0) {
                    String f = s.substring(l - 8, l);
                    bytes[i--] = (byte)Integer.parseInt(f, 2);
                }
            }

            return bytes;
        } else {
            return new byte[0];
        }
    }

}
